#include <bits/stdc++.h>

using namespace std;

const int N = 2010;

int n, a[N], dp[N][N];

int &setmax(int &x, int y) {
    if(y > x) x = y;
    return x;
}

void solve() {
    memset(dp, 0, sizeof dp);
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i];
    for(int i = 1; i <= n; i++) {
        for(int j = 1; j < i - 1; j++) {
            setmax(dp[i][i - 1], dp[i - 1][j] + (a[i] == a[j]) * a[i]);
            setmax(dp[i][j], dp[i - 1][j] + (a[i] == a[i - 1]) * a[i]);
        }
    }
    int ans = 0;
    for(int i = 1; i < n; i++) setmax(ans, dp[n][i]);
    cout << ans << "\n";
}

int main() {
    freopen("color.in", "r", stdin);
    freopen("color.out", "w", stdout);
    ios::sync_with_stdio(false);
    int T;
    cin >> T;
    while(T--) solve();
    return 0;
}